package com.mlk.core.util;

import com.fasterxml.jackson.core.type.TypeReference;
import com.mlk.core.config.UserConfig;
import com.mlk.rbac.po.SysUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.lang.reflect.Type;

/**
 * 用户工具类
 */
@Component
public class UserUtils {

    private UserUtils() {
    }

    private static RedisTemplate<String, String> redisTemplate;


    @Autowired
    public void setRedisTemplate(RedisTemplate<String, String> redisTemplate) {
        UserUtils.redisTemplate = redisTemplate;
    }

    /**
     * 通过用户的id,查询用户的信息
     * @param id
     * @return
     */
    public static final SysUser getSysUserById(Integer id) {
        String userStr = redisTemplate.opsForValue().get(UserConfig.CACHE_PREFIX + id);
        if (StringUtils.isEmpty(userStr)) {
            return null;
        }
        return JSONUtils.jSONstringToObj(userStr, new TypeReference<SysUser>() {});
    }
}
